package task

import (
	"crypto/sha256"
	"fmt"
	"net"
	"strings"

	"github.com/StackExchange/wmi"
)

type cpuInfo struct {
	Name        string
	UniqueId    string
	ProcessorId string
}

/*
	获取CPU信息
*/
func GetCPUInfo() string {
	cpustr := ""
	var cpuinfo []cpuInfo
	err := wmi.Query("Select * from Win32_Processor", &cpuinfo)
	if err != nil {
		return cpustr
	}
	fmt.Println("cpuinfo", cpuinfo)
	for _, v := range cpuinfo {
		cpustr += v.ProcessorId
	}
	return cpustr
}

type intfInfo struct {
	Name       string
	MacAddress string
	Ipv4       []string
}

/*
	获取网卡信息
*/
func GetNetworkInfo() string {
	macstr := ""
	intf, err := net.Interfaces()
	if err != nil {
		return macstr
	}
	var is = make([]intfInfo, len(intf))
	for i, v := range intf {
		_, err := v.Addrs()
		if err != nil {
			return macstr
		}
		//此处过滤loopback（本地回环）和isatap（isatap隧道）
		if !strings.Contains(v.Name, "Loopback") && !strings.Contains(v.Name, "isatap") {
			is[i].Name = v.Name
			is[i].MacAddress = v.HardwareAddr.String()
			if !strings.Contains(is[i].MacAddress, "00:ff") {
				macstr += is[i].MacAddress
			}

		}
	}
	return macstr
}

/*
	调用返回 CPUiD + MAC 地址 sha256 字符串
*/
func Uniquehost() string {
	fmt.Println("Cpu信息")
	cpu := GetCPUInfo()
	mac := GetNetworkInfo()
	fmt.Println("测试获取mac信息", mac)
	fmt.Println("测试获取cpu信息", cpu)
	sum := sha256.Sum256([]byte(cpu + mac))
	sha := fmt.Sprintf("%x", sum)
	return sha
}
